<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.6: http://docutils.sourceforge.net/" />
<title>Factory</title>
<style type="text/css">

/*
Author: Peter Parente
Date: 2008/01/22
Version: 1.0
Copyright: This stylesheet has been placed in the public domain - free to edit and use for all uses.
*/

body {
  font: 100% serif;
  background: #ffffff;
  color: black;
  margin: 2em;
  padding: 0em 2em;
  background-color: honeydew;
}

p.topic-title {
  font-weight: bold;
}

table.docinfo {
  text-align: left;
  margin: 2em 0em;
}

a[href] {
  color: #436976;
  background-color: transparent;
}

a.toc-backref {
  text-decoration: none;
}

h1 a[href] {
  color: #003a6b;
  text-decoration: none;
  background-color: transparent;
}

a.strong {
  font-weight: bold;
}

img {
  margin: 0;
  border: 0;
}

p {
  margin: 0.5em 0 1em 0;
  line-height: 1.5em;
}

p a:visited {
  color: purple;
  background-color: transparent;
}

p a:active {
  color: red;
  background-color: transparent;
}

a:hover {
  text-decoration: none;
}

p img {
  border: 0;
  margin: 0;
}

p.rubric {
  font-weight: bold;
  font-style: italic;
}

h1.title {
  color: #003a6b;
  font-size: 250%;
  margin-bottom: 0em;
}

h2.subtitle {
  color: #003a6b;
  border-bottom: 0px;
}

h1, h2, h3, h4, h5, h6 {
  color: #555;
  background-color: transparent;
  margin: 0em;
  padding-top: 0.5em;
}

h1 {
  font-size: 160%;
  margin-bottom: 0.5em;
  border-bottom: 2px solid #aaa;
}

h2 {
  font-size: 140%;
  margin-bottom: 0.5em;
  border-bottom: 1px solid #aaa;
}

h3 {
  font-size: 130%;
  margin-bottom: 0.5em;
}

h4 {
  font-size: 110%;
  font-weight: bold;
  margin-bottom: 0.5em;
}

h5 {
  font-size: 105%;
  font-weight: bold;
  margin-bottom: 0.5em;
}

h6 {
  font-size: 100%;
  font-weight: bold;
  margin-bottom: 0.5em;
}

dt {
  font-style: italic;
}

dd {
  margin-bottom: 1.5em;
}

div.admonition, div.note, div.tip, div.caution, div.important {
  margin: 2em 2em;
  padding: 0em 1em;
  border-top: 1px solid #aaa;
  border-left: 1px solid #aaa;
  border-bottom: 2px solid #555;
  border-right: 2px solid #555;
}

div.important {
  background: transparent url('../images/important.png') 10px 2px no-repeat;
}

div.caution {
  background: transparent url('../images/caution.png') 10px 2px no-repeat;
}

div.note {
  background: transparent url('../images/note.png') 10px 2px no-repeat;
}

div.tip {
  background: transparent url('../images/tip.png') 10px 2px no-repeat;
}

div.admonition-example {
  background: transparent url('../images/tip.png') 10px 2px no-repeat;
}

div.admonition-critical-example {
  background: transparent url('../images/important.png') 10px 2px no-repeat;
}

p.admonition-title {
  font-weight: bold;
  border-bottom: 1px solid #aaa;
  padding-left: 30px;
}

table.docutils {
  text-align: left;
  border: 1px solid gray;
  border-collapse: collapse;
  width: 100%;
  margin: 1.5em 0em;
}

table.docutils caption {
  font-style: italic;
}

table.docutils td, table.docutils th {
  padding: 0.25em 0.5em;
}

table.docutils th {
  background-color: #dddddd;
}

div.sidebar {
  width: 33%;
  float: right;
  margin: 0em 2em;
  padding: 0em 1em;
  border-top: 1px solid #aaa;
  border-left: 1px solid #aaa;
  border-bottom: 2px solid #555;
  border-right: 2px solid #555;
}

p.sidebar-title {
  margin-bottom: 0em;
  color: #003a6b;
  border-bottom: 1px solid #aaa;
  font-weight: bold;
}

p.sidebar-subtitle {
  margin-top: 0em;
  font-style: italic;
  color: #003a6b;
}

pre.literal-block, pre.doctest-block {
  border: thin black solid;
  padding: 5px;
  background-color: lightsteelblue;
}

code, tt {
  color: #000066;
}
</style>
</head>
<body>
<div class="document" id="factory">
<h1 class="title">Factory</h1>

<p>Factory is an object-oriented approach to partial function application, also known as currying. The factory module is a more Pythonic implementation of this pattern. Some improvements include:</p>
<ul class="simple">
<li>safer, as invalid arguments are detected immediately, instead of at call time</li>
<li>intelligent support for classes, instance methods &amp; all other callables</li>
<li>bound arguments can be inspected and modified as attributes</li>
<li>no &quot;Russian dolls&quot; of nested lambdas</li>
<li>several convenient methods for (re)binding arguments</li>
</ul>
<p>Using Factories can:</p>
<ul class="simple">
<li>ease functional programming</li>
<li>simplify writing callbacks</li>
<li>reduce bugs in concurrent applications</li>
<li>provide easy lazy evaluation</li>
</ul>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#about-currying" id="id1">About Currying</a></li>
<li><a class="reference internal" href="#releasing-the-insides" id="id2">Releasing the Insides</a></li>
<li><a class="reference internal" href="#early-error-detection" id="id3">Early Error Detection</a></li>
<li><a class="reference internal" href="#no-russian-dolls" id="id4">No Russian dolls</a></li>
</ul>
</div>
<pre class="doctest-block">
&gt;&gt;&gt; import functools
&gt;&gt;&gt; import itertools
</pre>
<div class="section" id="about-currying">
<h1><a class="toc-backref" href="#id1">About Currying</a></h1>
<p>Currying creates a new function from an existing one by binding some of the original's arguments:</p>
<pre class="doctest-block">
&gt;&gt;&gt; def adder(x, y):
...     return x + y
&gt;&gt;&gt; add_lambda = lambda y: adder(1, y)
&gt;&gt;&gt; add_lambda #doctest: +ELLIPSIS
&lt;function &lt;lambda&gt; at ...&gt;
&gt;&gt;&gt; add_lambda(10)
11
</pre>
<p>As of Python 2.5, this pattern is built in with the <a class="reference external" href="http://docs.python.org/whatsnew/2.5.html#pep-309-partial-function-application">partial</a> function.</p>
<pre class="doctest-block">
&gt;&gt;&gt; add_partial = functools.partial(adder, 1)
&gt;&gt;&gt; add_partial  #doctest: +ELLIPSIS
&lt;functools.partial object at ...&gt;
&gt;&gt;&gt; add_partial(y=10)
11
</pre>
</div>
<div class="section" id="releasing-the-insides">
<h1><a class="toc-backref" href="#id2">Releasing the Insides</a></h1>
<p>Factories are a more Pythonic pattern:</p>
<pre class="doctest-block">
&gt;&gt;&gt; from Factory import Factory as factory
&gt;&gt;&gt; add_factory = factory(adder, x=1)
&gt;&gt;&gt; add_factory #doctest: +ELLIPSIS,+SKIP
&lt;factory(&lt;function adder at ...&gt;) at ...&gt;
&gt;&gt;&gt; add_factory(y=10)
11
</pre>
<p>Factories can be inspected and modified:</p>
<pre class="doctest-block">
&gt;&gt;&gt; add_factory.x
1
&gt;&gt;&gt; add_factory.x = 2
&gt;&gt;&gt; add_factory(y=10)
12
</pre>
</div>
<div class="section" id="early-error-detection">
<h1><a class="toc-backref" href="#id3">Early Error Detection</a></h1>
<p>Factories ensure that attributes match up with arguments; this makes
finding errors easier (instead of raising a <tt class="docutils literal"><span class="pre">unexpected</span> <span class="pre">keyword</span> <span class="pre">argument</span></tt>
later):</p>
<pre class="doctest-block">
&gt;&gt;&gt; add_factory.pants = 42  #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
AttributeError: 'No such argument pants'
</pre>
</div>
<div class="section" id="no-russian-dolls">
<h1><a class="toc-backref" href="#id4">No Russian dolls</a></h1>
<p>An existing factory can be passed as the <tt class="docutils literal"><span class="pre">callee</span></tt> of a new factory.</p>
<pre class="doctest-block">
&gt;&gt;&gt; plus_factory = factory(add_factory, x = 10)
&gt;&gt;&gt; plus_factory is not add_factory
True
&gt;&gt;&gt; plus_factory.x
10
&gt;&gt;&gt; plus_factory.x = 100
</pre>
<p>Unlike using lambdas, this does not create nested &quot;Russian dolls&quot;:</p>
<pre class="doctest-block">
&gt;&gt;&gt; plus_factory.getCallable() #doctest: +ELLIPSIS
&lt;function adder at ...&gt;
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; add_factory(y=10)
12
&gt;&gt;&gt; plus_factory(y=10)
110
</pre>
</div>
</div>
</body>
</html>
